﻿using System;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Xml.Serialization;

namespace Mido.UiModules.SettingsModule
{
    public class SettingsModel
    {
        internal event ThreadStart Changed;

        private string _text = "http://mido.codeplex.com";
        private string _fontName = "Segoe UI";
        private int _fontSize = 5;
        private Color _color = Colors.White;
        private Color _stroke = Colors.Black;
        private int _strokeWidth = 1;
        private WatermarkSide _side = WatermarkSide.RightBottom;
        private int _maxWidth = 300;
        private int _maxHeight = 300;
        private bool _isBold = true;
        private bool _isItalic = false;
        private bool _isUnderline = false;
        private bool _isSettingsShown = true;
        private bool _keepOriginalSize = true;
        private string _imagePath;
        private ContentType _contentType = ContentType.OnlyText;
        private int _opacity = 100;
        private WatermarkAngle _watermarkAngle = WatermarkAngle.a0;
        private TextAlignment _textAlignment = TextAlignment.Center;
	    private FontSizeType _fontSizeType = FontSizeType.Percents;
	    private FontSizeType _imageSizeType = FontSizeType.Percents;
	    private int _relativeImageSize = 7;
	    private string _localization = string.Empty;

        public string Text
        {
            get { return _text; }
            set
            {
                if (_text == value)
                    return;

                _text = value;

                if (Changed != null)
                    Changed();
            }
        }

        public int Opacity
        {
            get { return _opacity; }
            set
            {
                if (_opacity == value)
                    return;

                _opacity = value;

                if (Changed != null)
                    Changed();
            }
        }

        public bool IsBold
        {
            get { return _isBold; }
            set 
            {
                if (_isBold == value)
                    return;

                _isBold = value;

                if (Changed != null)
                    Changed();
            }
        }

        public bool IsSettingsShown
        {
            get { return _isSettingsShown; }
            set { _isSettingsShown = value; }
        }

        public bool IsItalic
        {
            get { return _isItalic; }
            set
            {
                if (_isItalic == value)
                    return;

                _isItalic = value;

                if (Changed != null)
                    Changed();
            }
        }

		public FontSizeType FontSizeType
        {
			get { return _fontSizeType; }
            set
            {
				if (_fontSizeType == value)
                    return;

				_fontSizeType = value;

                if (Changed != null)
                    Changed();
            }
        }

		public FontSizeType ImageSizeType
        {
			get { return _imageSizeType; }
            set
            {
				if (_imageSizeType == value)
                    return;

				_imageSizeType = value;

                if (Changed != null)
                    Changed();
            }
        }

        public TextAlignment TextAlignment
        {
            get { return _textAlignment; }
            set
            {
                if (_textAlignment == value)
                    return;

                _textAlignment = value;

                if (Changed != null)
                    Changed();
            }
        }

        public bool IsUnderline
        {
            get { return _isUnderline; }
            set
            {
                if (_isUnderline == value)
                    return;

                _isUnderline = value;

                if (Changed != null)
                    Changed();
            }
        }

        public int StrokeWidth
        {
            get { return _strokeWidth; }
            set
            {
                if (_strokeWidth == value)
                    return;

                _strokeWidth = value;

                if (Changed != null)
                    Changed();
            }
        }

		public int RelativeImageSize
        {
			get { return _relativeImageSize; }
            set
            {
				if (_relativeImageSize == value)
                    return;

				_relativeImageSize = value;

                if (Changed != null)
                    Changed();
            }
        }

        public WatermarkAngle WatermarkAngle
        {
            get { return _watermarkAngle; }
            set
            {
                if (_watermarkAngle == value)
                    return;

                _watermarkAngle = value;

                if (Changed != null)
                    Changed();
            }
        }

        public bool KeepOriginalSize
        {
            get { return _keepOriginalSize; }
            set
            {
                if (_keepOriginalSize == value)
                    return;

                _keepOriginalSize = value;

                if (Changed != null)
                    Changed();
            }
        }

        public string FontName
        {
            get { return _fontName; }
            set
            {
                if (_fontName == value)
                    return;

                _fontName = value;

                if (Changed != null)
                    Changed();
            }
        }

        public int FontSize
        {
            get { return _fontSize; }
            set
            {
                if (_fontSize == value)
                    return;

                _fontSize = value;

                if (Changed != null)
                    Changed();
            }
        }

        public string ImagePath
        {
            get { return _imagePath; }
            set
            {
                if (_imagePath == value)
                    return;

                _imagePath = value;

                if (Changed != null)
                    Changed();
            }
        }

        public Color Color
        {
            get { return _color; }
            set
            {
                if (_color == value)
                    return;

                _color = value;

                if (Changed != null)
                    Changed();
            }
        }

        public Color Stroke
        {
            get { return _stroke; }
            set
            {
                if (_stroke == value)
                    return;

                _stroke = value;

                if (Changed != null)
                    Changed();
            }
        }

        public WatermarkSide Side
        {
            get { return _side; }
            set
            {
                if (_side == value)
                    return;

                _side = value;

                if (Changed != null)
                    Changed();
            }
        }

        public ContentType ContentType
        {
            get { return _contentType; }
            set
            {
                if (_contentType == value)
                    return;

                _contentType = value;

                if (Changed != null)
                    Changed();
            }
        }

        public int MaxWidth
        {
            get { return _maxWidth; }
            set
            {
                if (_maxWidth == value)
                    return;

                _maxWidth = value;

                if (Changed != null)
                    Changed();
            }
        }

        public int MaxHeight
        {
            get { return _maxHeight; }
            set
            {
                if (_maxHeight == value)
                    return;

                _maxHeight = value;

                if (Changed != null)
                    Changed();
            }
        }

        private static string FilePath
        {
            get
            {
                const string fileName = "settings.xml";

                string rootFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                string homeFolder = Path.Combine(rootFolder, "Mido");
                string fullFilePath = Path.Combine(homeFolder, fileName);

                if (!Directory.Exists(homeFolder))
                    Directory.CreateDirectory(homeFolder);

                return fullFilePath;
            }
        }

	    public string Localization
	    {
		    get { return _localization; }
		    set { _localization = value; }
	    }

	    internal static SettingsModel Load()
        {
            string filePath = FilePath;

            if (!File.Exists(filePath))
                return new SettingsModel();

            XmlSerializer serializer = new XmlSerializer(typeof(SettingsModel));
            using (FileStream file = File.OpenRead(filePath))
            {
                return (SettingsModel)serializer.Deserialize(file);
            }
        }

        internal void Save()
        {
            string filePath = FilePath;
            
            XmlSerializer serializer = new XmlSerializer(typeof(SettingsModel));
            using (FileStream file = File.Create(filePath))
            {
                serializer.Serialize(file, this);
            }
        }
    }

    public enum WatermarkSide
    {
        LeftTop,
        CenterTop,
        RightTop,
        LeftCenter,
        CenterCenter,
        RightCenter,
        LeftBottom,
        CenterBottom,
        RightBottom
    }

    public enum ContentType
    {
        OnlyText,
        ImageBeforeText,
        TextBeforeImage,
        OnlyImage
    }

    public enum WatermarkAngle
    {
        a0, a90, a180, a270
    }

	public enum FontSizeType
	{
		Pixels,
		Percents
	}
}
